package cn.webrx.bs.service;

import cn.webrx.bs.db.DruidUtil;
import cn.webrx.bs.entity.Link;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-06 17:02
 */
public class LinkDaoImpl implements LinkDao {
    protected QueryRunner run = new QueryRunner(DruidUtil.getDataSource());
    public int pagesize = 2;
    public int currpage = 1;
    public long pagecount = 1;
    public String where;


    @Override
    public int add(Link link) {
        int num = 0;
        try {
            num = run.update("insert into bs_link values(null,?,?,?)", link.getTitle(), link.getUrl(), link.isShows());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Link> showAll() {
        List<Link> list = null;
        try {
            list = run.query("select * from bs_link where shows", new BeanListHandler<>(Link.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Link> queryAll() {
        List<Link> list = null;
        //List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        try {
            list = run.query("select * from bs_link", new BeanListHandler<>(Link.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int delById(int id) {
        int num = 0;
        try {
            num = run.update("delete from bs_link where id = ?", id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public int delByIds(String ids) {
        int num = 0;
        Connection conn = DruidUtil.getConn();
        try {
            PreparedStatement pst = conn.prepareStatement("delete from bs_link where id in (" + ids + ")");
            num = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public int updateShows(int id, boolean shows) {
        int num = 0;
        try {
            num = run.update("update bs_link set shows = ? where id = ?", shows, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public long count() {
        long num = 0l;
        try {
            num = run.query("select count(*) from bs_link", new ScalarHandler<Long>());
        } catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public long count(String where) {
        long num = 0l;
        try {
            num = run.query("select count(*) from bs_link "+where, new ScalarHandler<Long>());
        } catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Link> page(int currpage) {
        List<Link> list = null;
        //List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        try {
            list = run.query("select * from bs_link order by id desc limit ?,?", new BeanListHandler<>(Link.class), currpage * pagesize - pagesize, pagesize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public String pageInfo(){
        StringBuilder s = new StringBuilder("<ul class=\"pagination\">");
        //上一页
        if(currpage<=1){
            s.append(String.format("<li class=\"disabled\"><a href=\"javascript:void(0)\">&lt;上一页</a></li>"));
        }else{
            s.append(String.format("<li><a href=\"/admin/link?action=manager&p=%d\">&lt;上一页</a></li>",currpage-1));
        }

        int start = 1;
        int end = 10;
        if(currpage>6){
            start = currpage - 5;
            end = currpage + 4;
        }
        for(int i = start ;i<=end;i++){
            if(i>pagecount) break;
            if(currpage == i) {
                s.append(String.format("<li class=\"active\"><a href=\"javascript:void(0)\">%d</a></li>",i));
            }else{
                s.append(String.format("<li><a href=\"/admin/link?action=manager&p=%d\">%d</a></li>",i,i));
            }
        }

        //下一页
        if(currpage>=pagecount){
            s.append(String.format("<li class=\"disabled\"><a href=\"javascript:void(0)\">下一页&gt;</a></li>"));
        }else{
            s.append(String.format("<li><a href=\"/admin/link?action=manager&p=%d\">下一页&gt;</a></li>",currpage+1));
        }
        s.append("</ul>");
        return s.toString();
    }
}
